#pragma once
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>

#include "ArticleRegister.h"

using namespace std;

class Database
{
private:
	vector<ArticleRegister> db;
public:

	Database() { }

	~Database() { }

	void Add(ArticleRegister ar)
	{
		db.push_back(ar);
	}

	string List()
	{
		ostringstream oss;
		for (auto ar : db)
		{
			oss << ar.List() << endl;
		}
		return oss.str();
	}

	void ReadDBFromFile()
	{
		string str;
		ifstream fin;
		try
		{
			fin.open("db.txt");
			while (getline(fin, str))
			{
				ArticleRegister ar;
				ar.ReadFromFile(str);
				if (ar.getNewspaper() != "N/A")
				{
					db.push_back(ar);
				}
			}
		}
		catch (exception e)
		{
			cout << e.what() << endl;
		}
	}

	vector<string> SearchTitle(string searchTerm)
	{
		vector<string> v;
		for (auto ar : db)
		{
			for (auto s : ar.SearchTitle(searchTerm))
			{
				v.push_back(s);
			}
		}
		return v;
	}

	vector<string> SearchKeyword(string keyword)
	{
		vector<string> v;
		for (auto ar : db)
		{
			for (auto s : ar.SearchKeyword(keyword))
			{
				v.push_back(s);
			}
		}
		return v;
	}
};

